home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4392 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: news.clark.net!usenet
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question on syntax of pointer to const
  5. Date: Tue, 30 Jan 1996 03:43:09 GMT
  6. Organization: Clark Internet Services, Inc.
  7. Message-ID: <4ek468$jr1@clarknet.clark.net>
  8. References: <4ej9eg$lq6@agate.berkeley.edu>
  9. NNTP-Posting-Host: gusty-ppp.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: Forte Free Agent 1.0.82
  14.  
  15. parsons@vouvray.CS.Berkeley.EDU (David C. Parsons) wrote:
  16.  
  17. >In a declaration of a const reference or const pointer type, does it matter
  18. >in what the order the keyword const and the underlying type appear?  That
  19. >is, are
  20.  
  21. >(1)    const double *pc;    and
  22. >(2)    double const *pc;
  23.  
  24. >both acceptable syntax for type "pointer to constant double"?  (My book only
  25. >describes (1), but my compiler accepts either.)
  26.  
  27. This is one of my favorite questions to ask C/C++ programmers at job
  28. interviews.
  29.  
  30. They are different. The first is a pointer to a constant double.
  31. Whatever pc points to is treated as a constant when referenced via pc.
  32. That is, it prevents you from making assignments like
  33.  
  34.     *pc = 3.14;
  35.  
  36. The second is a constant pointer to a double. You can change the value
  37. of the double that pc points to all you want, but the pointer itself
  38. can only ever point to one place in memory, and you have to specify
  39. where that is at declaration time. Just as you can't have
  40.  
  41.     const double threepointtwo;
  42.     threepointtwo = 3.2;
  43.  
  44. but need
  45.  
  46.     const double threepointtwo = 3.2;
  47.  
  48. you likewise have to have something like
  49.  
  50.     double arrayOfDoubles[25];
  51.     double const pc = arrayOfDoubles;
  52.  
  53. or
  54.  
  55.     double threepointtwo = 3.2;
  56.     double const pc = &threepointtwo;
  57.  
  58. You can even have const double const pc, a constant pointer to a
  59. constant double. I'm not sure what good it is.
  60.  
  61.  
  62.